package com.tim.app.ui.adapter.story;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.application.library.base.LSAdapter;
import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.DynamicItem;
import com.tim.app.util.BitmapLoader;


/**
 * 发布动态选图
 */

public class DynamicImageAdapter extends LSAdapter {

    Context context;

    public DynamicImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        GoodViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_dynamic_image, null);
            holder = new GoodViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GoodViewHolder) convertView.getTag();
        }
        final DynamicItem item = (DynamicItem) getItem(position);

        if (DynamicItem.TYPE_ADD == item.getType()) {
            holder.ivDelete.setVisibility(View.GONE);
            holder.rlAddContainer.setVisibility(View.VISIBLE);
            holder.ivDynamicImage.setVisibility(View.GONE);
        } else {
            holder.ivDelete.setVisibility(View.VISIBLE);
            holder.rlAddContainer.setVisibility(View.GONE);
            holder.ivDynamicImage.setVisibility(View.VISIBLE);
            BitmapLoader.ins().loadLocalImage(item.getPath(),holder.ivDynamicImage);
        }
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventManager.ins().sendEvent(EventTag.DELETE_DYNAMIC_IMAGE, 0, 0, position);
            }
        });
        return convertView;
    }

    static class GoodViewHolder {
        ImageView ivDynamicImage;
        ImageView ivDelete;
        RelativeLayout rlAddContainer;

        public GoodViewHolder(View itemView) {
            ivDynamicImage = (ImageView) itemView.findViewById(R.id.ivDynamicImage);
            ivDelete = (ImageView) itemView.findViewById(R.id.ivDelete);
            rlAddContainer = (RelativeLayout) itemView.findViewById(R.id.rlAddContainer);
        }
    }
}